let express = require('express')
let fs = require('fs-extra')
let app = express()

// 处理跨域
app.all('*', (req, res, next) => {
  res.header('Access-Control-Allow-Origin', '*')
  res.header(
    'Access-Control-Allow-Headers',
    'Content-Type,Content-Length, Authorization, Accept,X-Requested-With, Range'
  )
  res.header('Access-Control-Allow-Methods', 'PUT,POST,GET,DELETE,OPTIONS')
  res.header('X-Powered-By', ' 3.2.1')
  if (req.method === 'OPTIONS') res.send(200) /*让options请求快速返回*/
  else next()
})

// 长度
app.get('/length', (req, res, next) => {
  res.end('' + fs.statSync('./1.png').size)
})

//
app.get('/', (req, res, next) => {
  res.download('1.png', {
    acceptRanges: true
  })
})

app.listen(1112, () => {
  console.log('服务启动完成，端口监听1112！')
})
